#region Imported Namespaces
using System;
using System.Collections;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;

using SSRS;
#endregion

/// <summary>
/// All the SQL report host pages will use this page to request the report using the reporting web service
/// </summary>
namespace PHSRAG.Insight21
{
    
    public class SSRSRequest
    {
        public class ReportParameterKeys
        {
            public  const string WebUrl = "weburl";
            public  const string FilterCriteria = "filtercriteria";
            public  const string ReportTitle = "reporttitle";
            public  const string UserName = "username";
            public  const string DomainUserId = "domainuserid";
            public const string SessionId = "SessionId";
			public const string DataSource = "datasource";
			public const string SalaryAsOfDate = "salaryasofdate";

        }

        public class ReportNames
        {
            public class Humans
            {
                public const string ActiveProtocols = "humans_activeprotocols";
                public const string InActiveProtocols = "humans_inactiveprotocols";
                public const string WorkInProgress = "humans_workinprogress";
                public const string ExemptProtocols = "humans_exemptprotocols";
                public const string Protocols = "humans_protocols";
                public const string PendingApplications = "humans_pendingapplications";
                public const string HumansManagement = "humans_mgmt";
            }
            public class Animals
            {
                public const string ActiveProtocols = "animals_activeprotocols";
                public const string InActiveProtocols = "animals_inactiveprotocols";
                public const string WorkInProgress = "animals_workinprogress";
                public const string ExemptProtocols = "animals_exemptprotocols";               
                public const string PendingApplications = "animals_pendingapplications";
            }

            public class ClinicalTrials
            {
                public const string ProcedureVisits = "CT_ProcedureVisit";
                public const string BudgetVisits = "CT_BudgetVisit";
                public const string ExternalOutput = "CT_ExternalOutput";
                public const string InternalOutput = "CT_InternalOutput";                
            }
            public class Purchasing
            {
                public const string PODetailsCollapsed = "Pur_PODetailsCollapsed";
                //Matched
                public const string ReconciledMatchedPO = "Pur_ReconcileMatched_PO";
                public const string ReconciledMatchedAP = "Pur_ReconcileMatched_AP";
                //Unmatched
                public const string ReconciledUnMatchedAP = "Pur_ReconcileUnMatched_AP";
                public const string ReconciledUnMatchedJE = "Pur_ReconcileUnMatched_JE";
                public const string ReconciledUnMatchedPO = "Pur_ReconcileUnMatched_PO";
            }
            public class Personnel
            {
                public const string ExpiredFundAllocation = "personnel_expiredfundallocation";
                public const string ExceptionReport = "personnel_exceptionreport";
                public const string SalaryGrid = "personnel_salarygrid";
                public const string PersonnelDetails = "personnel_personneldetails";
                public const string PersonnelSummary = "personnel_personnelsummary";
				public const string PayrollReport = "personnel_payrollreport";
            }
            public class Coi
            {
                public const string SupervisorSummary = "Coi_SupervisorSummary";
                public const string CustomReports = "Coi_CustomReports";
            }
            public class Common
            {
                public const string POAPOrderSummary = "Cmn_POAPOrderSummary";
                public const string POAPOrderDetail = "Cmn_POAPOrderDetail";                
            }

            
        }

        public SSRSRequest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        static ParameterValue[] GetParametersCollection(Hashtable tbl)
        {
            ParameterValue[] oInputPrms = new ParameterValue[tbl.Count];

            int i = 0;            
            foreach (string key in tbl.Keys)
            {
                ParameterValue p = new ParameterValue();
                p.Name = key;
                if (key == ReportParameterKeys.FilterCriteria)
                {
                    p.Value = ((string)tbl[key]).Replace("|", ": "); //The report rquirement is to not have the pipe character in the filter criteria.
                }
                else p.Value = (string)tbl[key];

                oInputPrms[i] = p;

                i++;
            }

            return oInputPrms;
        }

        public static void FetchAndDisplayReport(string format, string RptName, Hashtable tbl)
        {
            ParameterValue[] InputParams = GetParametersCollection(tbl);
            FetchAndDisplayReport(format, RptName, InputParams);
        }

        /*This method will fetch the data and write it to the browser
         * The method can be called from the SQL Report Host pages
         */
        public static void FetchAndDisplayReport(string format, string RptName, ParameterValue[] InputParams)
        {
            SSRS.ReportingService oRS = new ReportingService();
            //ReportingService oRS = new ReportingService();

            //oRS.Credentials = new System.Net.NetworkCredential("ys985", "fun2work", "hornet");
            NetworkCredential oCred = new NetworkCredential(
                Utility.Settings.GetKeyValue("Rpt.UserId", "pn073"),
                Utility.Settings.GetKeyValue("Rpt.Password", "Insight214"),
                //Utility.Settings.GetKeyValue("Rpt.Domain", "hornet"));
                Utility.Settings.GetKeyValue("Rpt.Domain", "partners"));

            oRS.Credentials = oCred;

            
            string encoding, mimetype;
            ParameterValue[] oOutParams;
            Warning[] oWarnings;
            string[] stringids;

            string TargetReportFolder = Utility.Settings.GetKeyValue("Rpt.TargetReportFolder", @"/insight/Prod/");
            byte[] data = oRS.Render(TargetReportFolder + RptName, format, null, null, InputParams,
                null, null, out encoding, out mimetype, out oOutParams, out oWarnings, out stringids);

            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.AddHeader("Content-Length", data.Length.ToString());
            HttpContext.Current.Response.AddHeader("Expires", "0");
            HttpContext.Current.Response.AddHeader("Pragma", "cache");
            HttpContext.Current.Response.AddHeader("Cache-Control", "private");

            //Set the output stream to the correct content type (PDF).
            if (format == "PDF")
            {
                HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename= PDFReport.pdf");
                HttpContext.Current.Response.ContentType = "application/pdf";
            }
            else
            {
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=ExcelReport.xls");
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            }
            HttpContext.Current.Response.BinaryWrite(data);

            //Flushing the Response to display the serialized data
            //to the client browser.
            HttpContext.Current.Response.Flush();
        }       
    } 
}
